package com.yuchen.advsdk.weight.marqueeview;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

import com.yuchen.advsdk.R;


public class HorizontalMarqueeView extends HorizontalScrollView {


    private static final String TAG = "HorizontalMarqueeView";

    private int andTextColor;
    private int andTextSize;
    private int andTextBbackgroundColor;
    private boolean andTextAnimUp;
    private boolean andTextAnimDown;
    private boolean andTextAnimLeft;
    private boolean andTextAnimRight;
    private int aniDuration;
    private String andTextDesc;

    private TextView andTextView;

    public HorizontalMarqueeView(Context context) {
        this(context, null);
        init();
    }

    public HorizontalMarqueeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalMarqueeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AndTextViewLayout, defStyleAttr, R.style.def_and_text_layout);
//        int indexCount = typedArray.getIndexCount();
//        for (int i = 0; i < indexCount; i++) {
//            int attr = typedArray.getIndex(i);
//            if (attr == R.styleable.AndTextViewLayout_and_text_color) {
//                andTextColor = typedArray.getColor(attr, 0);
//
//            } else if (attr == R.styleable.AndTextViewLayout_and_text_size) {
//                andTextSize = typedArray.getInteger(attr, 0);
//
//            } else if (attr == R.styleable.AndTextViewLayout_and_text_background_color) {
//                andTextBbackgroundColor = typedArray.getColor(attr, 0);
//
//            } else if (attr == R.styleable.AndTextViewLayout_and_text_anim_duration_second) {
//                aniDuration = typedArray.getInteger(attr, 0) * 1000;
//
//            } else if (attr == R.styleable.AndTextViewLayout_and_text_anim_up) {
//                andTextAnimUp = typedArray.getBoolean(attr, false);
//
//            } else if (attr == R.styleable.AndTextViewLayout_and_text_anim_down) {
//                andTextAnimDown = typedArray.getBoolean(attr, false);
//
//            } else if (attr == R.styleable.AndTextViewLayout_and_text_anim_left) {
//                andTextAnimLeft = typedArray.getBoolean(attr, false);
//
//            } else if (attr == R.styleable.AndTextViewLayout_and_text_anim_right) {
//                andTextAnimRight = typedArray.getBoolean(attr, true);
//
//            } else if (attr == R.styleable.AndTextViewLayout_and_text_desc) {
//                andTextDesc = typedArray.getString(attr);
//
//            }
//        }
//        typedArray.recycle();
//        initAndTextView();
        init();
    }

    private void init() {
        andTextColor = getContext().getResources().getColor(R.color.black);

        andTextSize = 20;

        andTextBbackgroundColor = getContext().getResources().getColor(R.color.tans);


        aniDuration = 12 * 1000;

        andTextAnimUp = false;

        andTextAnimDown = false;

        andTextAnimLeft = true;

        andTextAnimRight = false;


        andTextDesc = "请设置跑马灯内容";
        initAndTextView();
    }

    /**
     * 字体大小 单位 px
     *
     * @param sizePx
     */
    public void setAndTextSize(int sizePx) {
        andTextSize = sizePx;
        andTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, andTextSize);
    }

    /**
     * 字体颜色
     *
     * @param andTextColor
     */
    public void setAndTextColor(int andTextColor) {
        this.andTextColor = andTextColor;
        andTextView.setTextColor(andTextColor);
    }

    /**
     * 字体背景颜色
     */
    public void setAndTextBbackgroundColor(int andTextBbackgroundColor) {
        this.andTextBbackgroundColor = andTextBbackgroundColor;
        andTextView.setBackgroundColor(andTextBbackgroundColor);
    }


    private void initAndTextView() {
        setHorizontalScrollBarEnabled(false);
        andTextView = new TextView(getContext());
        LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        andTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, andTextSize);
        andTextView.setTextColor(andTextColor);
//        andTextView.setText(andTextDesc);
        andTextView.setMaxLines(1);
        andTextView.setBackgroundColor(andTextBbackgroundColor);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        andTextView.setLayoutParams(layoutParams);

        addView(andTextView);

        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

    }

    public interface Direction {
        int LEFT = 1;
        int RIGHT = 2;
    }

    /**
     * 设置方向
     *
     * @param direction
     */
    public void setTextAnimDirection(int direction) {
        if (direction == Direction.LEFT) {
            andTextAnimLeft = true;
            andTextAnimRight = false;
        } else if (direction == Direction.RIGHT) {
            andTextAnimLeft = false;
            andTextAnimRight = true;
        } else {
            andTextAnimLeft = true;
            andTextAnimRight = false;
        }
    }

    public void setAnimDuration(int duration) {
        this.aniDuration = duration * 1000;
    }

    public void setMarqueeTv(String tv) {
        andTextDesc = tv;
        if (andTextView != null) {
            andTextView.setText(tv);
        }
//        andTextView.setText("");
        andTextView.setAlpha(0);
    }


    public void startAndTextAnim() {

        andTextView.setText(andTextDesc);
        andTextView.setAlpha(1);
        if (getWidth() != 0 && getHeight() != 0) {
            Log.d(TAG, aniDuration + "");
            ObjectAnimator objectAnimator = creatCurrentAnimation();
            objectAnimator.setDuration(aniDuration);
            objectAnimator.setInterpolator(new LinearInterpolator());
            objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
            objectAnimator.start();
        }
    }

    public void stopAndTextAnim() {
        andTextView.setAlpha(0);
        ObjectAnimator objectAnimator = (ObjectAnimator) andTextView.getTag();
        if (objectAnimator != null) {
            objectAnimator.cancel();
        }
    }

    private ObjectAnimator creatCurrentAnimation() {
        ObjectAnimator objectAnimator = null;
        if (andTextAnimRight) {
            objectAnimator = ObjectAnimator.ofFloat(andTextView, "translationX", -andTextView.getMeasuredWidth(), getWidth());
        } else if (andTextAnimLeft) {
            objectAnimator = ObjectAnimator.ofFloat(andTextView, "translationX", getWidth(), -andTextView.getMeasuredWidth());
//            Log.d(TAG,getWidth()+" : "+(-andTextView.getMeasuredWidth()));
        } else if (andTextAnimUp) {
            objectAnimator = ObjectAnimator.ofFloat(andTextView, "translationY", -getHeight(), getHeight() + andTextView.getMeasuredHeight());
        } else if (andTextAnimDown) {
            objectAnimator = ObjectAnimator.ofFloat(andTextView, "translationY", getHeight() + andTextView.getMeasuredHeight(), -getHeight());
        }
        andTextView.setTag(objectAnimator);
        return objectAnimator;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
//        startAndTextAnim();
    }
}
